<?php 
	namespace Admin\Controller;
	use Think\Controller;

	class WebConfigController extends Controller
	{
		public function index()
		{
			$data = M('webconfig');
			$list = $data->order('id desc')->select();
			$this->assign('list',$list);
			$this->display('WebConfig/index');
		}

		public function add()
		{
			$this->display('WebConfig/add');
		}
		public function doadd()
		{
			$data = M('webconfig');
			if($_POST['title'] == "" || $_POST['word'] == "" || $_POST['copyright'] == "" || $_POST['description'] == "" || $_POST['registration'] == "" || $_POST['licence'] == "" || $_POST['certtext'] == ""){
				echo '<script>alert("添加失败,请完整添加数据");window.location.href="add"</script>';
				exit;
			}
			//处理logo
			$upload = new \Think\Upload();
			//实例化上传类
			$upload->maxSize = 3145728;
			//设置附件上传大小
			$upload->exts    = array('jpg','jpeg','png','gif');
			//设置附件上传目录
			$upload->rootPath= './Public';
			$upload->savePath= '/Uploads/';
			//上传文件
			$info = $upload->upload();
			//拼接图片的路径
			//头部logo
			$_POST['headlogo'] = trim($upload->rootPath,'.').$info['headlogo']['savepath'].$info['headlogo']['savename'];
			$_POST['footlogo'] = trim($upload->rootPath,'.').$info['footlogo']['savepath'].$info['footlogo']['savename'];

			//创建数据
			$data->create();
			if($data->add()){
				$this->success('恭喜你添加成功',U('index'),3);
			}else{
				$this->error('添加失败');
			}
		}

		public function del()
		{
			if (empty($_GET['id'])) {
            $this->redirect('Admin/WebConfig/index');
            exit;
        	}
	        //接收参数
	        // $id = $_GET['id'];
	        // I()方法 过滤输入的数据
	        $id = I('get.id/d');
	        // echo $id;exit;

	        //删除操作
	        if (M('webconfig')->delete($id) > 0) {
	            $this->success('恭喜您,删除成功!',U('index'));
	        } else {
	            $this->error('删除失败!!!!!');
	        }
		}

		//修改商城页面
		public function edit()
		{
			$data = M('webconfig');
			$list=$data->find($_POST['id']);
			$this->assign('list',$list);
			$this->display();

		}

		public function update()
		{
			$id   = $_POST['id'];
			$con = M('webconfig');
			$data = $con->find($id);

			//上传logo

			//实例化上传类
			$upload = new \Think\Upload();
			//设置附件上传大小
			$upload->maxSize = 3145728;
			//设置附件上传目录
			$upload->rootPath = './Public';
			$upload->savePath = '/Uploads/';
			//logo上传
			$info = $upload->upload();

			//判断头部LOGO是否有文件上传，如果没有则用原来的图片
			if($_FILES['headlogo']['error'] == 4){
				$_POST['headlogo'] = $data['headlogo'];
			}else{

				$_POST['headlogo'] = trim($upload->rootPath,'.').$info['headlogo']['savepath'].$info['headlogo']['savename'];
				//删除原来的图片
				unlink($data['headlogo']);
			}

			//判断尾部LOGO是否有文件上传，如果没有则用原来的图片
			if($_FILES['footlogo']['error'] == 4){
				$_POST['footlogo'] = $data['footlogo'];
			}else{

				$_POST['footlogo'] = trim($upload->rootPath,'.').$info['footlogo']['savepath'].$info['footlogo']['savename'];
				//删除原来的图片
				unlink($data['footlogo']);
			}

			if($con -> create()){
				if($con -> save()){
					$this -> redirect("WebConfig/index");
				}else{
					$this -> error("网站信息修改失败！");
				}
			}else{
				$this -> error("网站信息修改失败!");
			}
		}

	}